package yxy.game.pm2.command.therionHunting;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.TherionHuntingMessage;
import yxy.game.pm2.module.therionHunting.TherionHandle;
import yxy.game.pm2.module.therionHunting.TherionModule;

/**
 * @name CMD_TherionResurrectionRemind
 * @author wrs
 * @descrition 神兽复活提醒
 * @since 2022-06-16
 */
@MessageType(proto = Command.TherionResurrectionRemind ,classtype = TherionHuntingMessage.TherionResurrectionRemindRequest.class)
public class CMD_TherionResurrectionRemind extends Command<TherionHuntingMessage.TherionResurrectionRemindRequest> {

    @Override
    public void execute(MessageSource messageSource, TherionHuntingMessage.TherionResurrectionRemindRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int gameType = request.getGameType();
        int therionId = request.getTherionId();
        boolean isChecked = request.getIsChecked(); //是否勾选提醒

        TherionHuntingMessage.TherionResurrectionRemindResponse.Builder response = TherionHuntingMessage.TherionResurrectionRemindResponse.newBuilder();

        TherionHandle handle = new TherionHandle();
        try {
            int therionType = handle.getTherionKey(therionId);
            if (therionType == -1){
                send(messageSource,response, Result.DataError);
                return;
            }

            TherionModule therionModule = TherionModule.get(gameType, playerCode, therionType);
            if (therionModule == null){
                send(messageSource,response, Result.DataError);
                return;
            }

            Result result = handle.bookedTherion(therionModule,isChecked,playerCode);
            send(messageSource,response,result);

        }catch (Exception e){
            e.printStackTrace();
            send(messageSource,response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, TherionHuntingMessage.TherionResurrectionRemindResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
